/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2025/6/16
 * Time: 22:36
 */

#include "Stats.h"
#include "../core/Actor.h"
Stats * Stats::AddStatsChild(Actor * _parent, float _max_health, float _max_mana, float _damage, float _mana_regen)
{
    Stats * stats      = new Stats();
    stats->parent_     = _parent;
    stats->max_health_ = _max_health;
    stats->health_     = _max_health;
    stats->max_mana_   = _max_mana;
    stats->mana_       = _max_mana;
    stats->damage_     = _damage;
    stats->mana_regen_ = _mana_regen;
    _parent->AddChild(stats);
    return stats;
}

void Stats::Update(float _dt)
{
    // SDL_Log("sssss,%d",is_invincible_);
    Object::Update(_dt);
    RegenMana(_dt);
    if(is_invincible_) {
        invincible_timer_ += _dt;
        if(invincible_timer_ >= invincible_time_) {
            is_invincible_    = false;
            invincible_timer_ = 0;
        }
    }
}

bool Stats::CanUseMana(float _mana_cost)
{
    return mana_ >= _mana_cost;
}

void Stats::UseMana(float _mana_cost)
{
    mana_ -= _mana_cost;
    if(mana_ < 0)
        mana_ = 0;
}

void Stats::RegenMana(float _dt)
{
    mana_ += mana_regen_ * _dt;
    if(mana_ > max_mana_)
        mana_ = max_mana_;
}

void Stats::TakeDamage(float _damage)
{
    // if(is_invincible_)
    //     return;

    // SDL_Log("ddddd %f",_damage);
    health_ -= _damage;
    if(health_ < 0) {
        health_   = 0;
        is_alive_ = false;
    }
    SDL_Log("Health: %f", health_);
    is_invincible_    = true;
    invincible_timer_ = 0.0f;
}